package com.farm.system.service.impl;

import com.farm.constant.MessageConstant;
import com.farm.system.dao.MenuMapper;
import com.farm.system.pojo.Menu;
import com.farm.system.pojo.Role;
import com.farm.system.service.MenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * 菜单实现类
 *
 * @author Chengfei
 * @date 2020/10/11
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 查询所有菜单
     *
     * @return 菜单集合
     */
    @Override
    public List<Menu> findAll() {
        List<Menu> menuList = menuMapper.selectAll();
        //判断数据是否有效
        if (menuList == null || menuList.size() <= 0) {
            throw new RuntimeException(MessageConstant.SELECT_ALL_MENU_FAIL);
        }

        return menuList;
    }

    /**
     * 条件分页查询
     *
     * @param page      页码
     * @param size      大小
     * @param searchMap 条件
     * @return 菜单集合
     */
    @Override
    public Page<Menu> findPageByCondition(Integer page, Integer size, Map<String, Object> searchMap) {

        //开启分页
        PageHelper.startPage(page, size);

        //调用方法添加条件
        Example example = createExample(searchMap);
        List<Menu> menuList = menuMapper.selectByExample(example);

        //判断数据是否有效
        if (menuList == null || menuList.size() <= 0) {
            throw new RuntimeException(MessageConstant.SELECT_MENU_FAIL);
        }

        return (Page<Menu>) menuList;
    }

    /**
     * 条件构造器
     *
     * @param searchMap 条件
     * @return
     */
    public Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();

        //判断条件数据是否有效
        if (searchMap != null && searchMap.size() > 0) {

            //判断角色名是否存在,存在添加为模糊查询条件
            if (org.apache.commons.lang.StringUtils.isNotBlank((String) searchMap.get("menu"))) {
                criteria.andLike("menu", "%" + searchMap.get("menu") + "%");
            }
            //判断关键字是否存在,存在添加为模糊查询条件
            if (org.apache.commons.lang.StringUtils.isNotBlank((String) searchMap.get("type"))) {
                criteria.andEqualTo("type", searchMap.get("type"));
            }

        }
        return example;
    }

    /**
     * 添加菜单
     *
     * @param menu 菜单
     */
    @Override
    public void add(Menu menu) {
        //判断菜单数据是否有效
        if (menu == null) {
            throw new RuntimeException(MessageConstant.ADD_MENU_FAIL);
        }
        menuMapper.insertSelective(menu);

    }

    /**
     * 更新菜单
     *
     * @param menu 菜单
     */
    @Override
    public void update(Menu menu) {
        //判断菜单数据是否有效
        if (menu == null) {
            throw new RuntimeException(MessageConstant.UPDATE_MENU_FAIL);
        }
        menuMapper.updateByPrimaryKeySelective(menu);

    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单id
     */
    @Override
    public void delete(Integer menuId) {
        //判断该菜单是否被角色使用
        int count = menuMapper.selectCountByMenuId(menuId);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL);
        }
        //删除菜单
        menuMapper.deleteByPrimaryKey(menuId);
    }
}
